﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace Catering.DisplayBoards
{
    public partial class ContractorBoard : System.Web.UI.Page
    {
        Catering_Rules.Area tArea = new Catering_Rules.Area();//区域管理
        Catering_Rules.ClassInfo tClassInfo = new Catering_Rules.ClassInfo();//班组管理
        Catering_Rules.Contractor tContractor = new Catering_Rules.Contractor();//承包商字典
        Catering_Rules.ContractorReport tContractorReport = new Catering_Rules.ContractorReport();//承包商动态管理
        Catering_Rules.AttendanceRecord tAttendanceRecord = new Catering_Rules.AttendanceRecord();//日常执勤管理
        List<Catering_Model.AttendanceRecord> AttendanceRecordList = new List<Catering_Model.AttendanceRecord>(); //日报数据

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.RepDataBind();
            }
        }

        /// <summary>
        /// 数据绑定
        /// </summary>
        public void RepDataBind()
        {
            DateTime SearchDateTime = DateTime.Now.AddDays(-1);
            if (Request.QueryString["Date"] != null)
            {
                SearchDateTime = Convert.ToDateTime(Request.QueryString["Date"]);
            }
            //获取当日 日报数据
            AttendanceRecordList = tAttendanceRecord.GetModelList("(temp1='1' or temp2='1') and[Date]=CAST('" + SearchDateTime.ToShortDateString() + "' as datetime)");

            string[] ArrContraType = new string[] { "日常设备维护", "项目施工监管", "终端外包业务" };
            List<Catering_Model.Area> AreaList = tArea.GetModelList("");//区域列表
            List<Catering_Model.ClassInfo> ClassInfoList = tClassInfo.GetModelList("");//获取对应班组集合
            List<Catering_Model.Contractor> ContractorList=tContractor.GetModelList("");//获取承包商字典
            List<Catering_Model.ContractorReport> ContractorReportList = tContractorReport.GetModelList("AddDate=CAST('" + SearchDateTime.ToShortDateString() + "' as date)"); //获取所有班组对应的当日动态

            //获取数据源
            List<E_Contractor> list = new List<E_Contractor>();
            foreach (Catering_Model.ContractorReport eContractorReport in ContractorReportList)
            {
                E_Contractor Item = new E_Contractor();
                Catering_Model.Contractor eContractor = ContractorList.Where(p => p.id == eContractorReport.ContractorID).FirstOrDefault();
                if (eContractor != null)
                {
                    Item.ContraType = eContractor.ContraType;
                    Item.ContractorName = eContractor.CName;
                    Catering_Model.ClassInfo eClassInfo = ClassInfoList.Where(p => p.id == eContractorReport.ClassID).FirstOrDefault();
                    Item.ClassName = (eClassInfo != null) ? eClassInfo.CName : "";
                    Item.OptsNums = eContractorReport.optsNums;         //作业人数
                    Item.Content = eContractorReport.WorkContent;       //作业内容
                    Item.RIfication = eContractorReport.RiskContent;    //风险识别
                    Item.CMeasures = eContractorReport.ControlContent;  //控制措施
                    Catering_Model.AttendanceRecord eAttendanceRecord = AttendanceRecordList.Where(p => p.temp1 == "1" && p.ClassID == eContractorReport.ClassID).FirstOrDefault(); //获取管事人员
                    Item.CName = (eAttendanceRecord == null ? "" : eAttendanceRecord.PName);//管事
                    list.Add(Item);
                }
            }

            //拼接页面Html
            StringBuilder Html = new StringBuilder();
            foreach (string strContraType in ArrContraType)
            {
                List<Catering_Model.Contractor> TempContractorList = ContractorList.Where(p => p.ContraType == strContraType).ToList(); //获取日常维护承包商字典
                List<E_Contractor> TempMainList = list.Where(p => p.ContraType == strContraType).ToList(); // 获取日常维护日报数据
                int Index = 0; //整体行索引
                foreach (Catering_Model.Contractor eContractor in TempContractorList)
                {
                    int i = 0;//某单位行索引
                    List<E_Contractor> TempSubList = TempMainList.Where(p => p.ContractorName == eContractor.CName).ToList();
                    if (TempSubList != null && TempSubList.Count > 0)
                    {
                        foreach (E_Contractor Item in TempSubList)
                        {
                            Html.Append("<tr class=\"Table_Tr\">");
                            if (Index == 0)
                            {
                                Html.Append("<td class=\"T1\" rowspan=\"" + TempMainList.Count + "\">" + strContraType + "</td>");             //业务名称
                            }
                            if (i == 0)
                            {
                                Html.Append("<td class=\"T2\" rowspan=\"" + TempSubList.Count + "\">" + eContractor.CName + "</td>"); //外单位名称
                            }
                            Html.Append("<td class=\"T3\">" + Item.ClassName + "</td>");             //班组名称
                            Html.Append("<td class=\"T1\">" + Item.CName + "</td>");                 //管事
                            Html.Append("<td class=\"T2\">" + Item.OptsNums + "</td>");              //作业人数
                            Html.Append("<td class=\"T3\">" + Item.Content + "</td>");               //作业内容
                            Html.Append("<td class=\"T1\">" + Item.RIfication + "</td>");            //风险识别
                            Html.Append("<td class=\"T2\">" + Item.CMeasures + "</td>");             //控制措施
                            Html.Append("</tr>");
                            i++;
                            Index++;
                        }
                    }
                }
            }
            this.MainList.InnerHtml = Html.ToString();
            this.OptsNumsSum.InnerHtml = list.Sum(p => p.OptsNums).ToString();
            ScriptManager.RegisterClientScriptBlock(this.UpdatePanel1, Page.GetType(), "script", "<script>$(function () { $(\".Table_2>tbody tr:odd\").addClass(\"Table_Tr_odd\"); });</script>", false);
        }

        //定时刷新页面
        protected void Btn_Ref_Click(object sender, EventArgs e)
        {
            this.RepDataBind();
        }
    }


    public class E_Contractor
    {
        /// <summary>
        /// 业务名称：日常设备维护、项目施工监管、终端外包业务
        /// </summary>
        public string ContraType { get; set; }

        /// <summary>
        /// 承包商名称=外单位名称
        /// </summary>
        public string ContractorName { get; set; }

        /// <summary>
        /// 区域名称
        /// </summary>
        public string AreaName { get; set; }

        /// <summary>
        /// 班组名称
        /// </summary>
        public string ClassName { get; set; }

        /// <summary>
        /// 监管人
        /// </summary>
        public string CName { get; set; }

        /// <summary>
        /// 作业人数
        /// </summary>
        public int OptsNums { get; set; }

        /// <summary>
        /// 作业内容
        /// </summary>
        public string Content { get; set; }

        /// <summary>
        /// 风险识别
        /// </summary>
        public string RIfication { get; set; }

        /// <summary>
        /// 控制措施
        /// </summary>
        public string CMeasures { get; set; }
    }
}